<?php
include_once 'db_ini.php';

function getListDocuments($keyWords) {
	// On construit la liste des mots clefs
	$keyWordsString = join("', '", $keyWords);
	$keyWordsString = "'" . $keyWordsString . "'";
	$keyWordsString = strtolower($keyWordsString);
	
	// On récupère les documents pertinents
	$requete = "SELECT 
					nom, SUM(frequence) as poids, GROUP_CONCAT(radical) AS radicaux
				FROM
					document d,
					occurence o,
					term t
				WHERE
					d.id = o.id_document AND t.id = o.id_term AND t.radical IN (" . $keyWordsString . ")
				GROUP BY nom
				ORDER BY poids DESC";
	
	$statement = getConnexion()->query($requete);
	$results = array();
	while ($row = $statement->fetch()) {
		// On vérifie la présence de tous les mots de la requête
		$list_radicaux = split(",", $row["radicaux"]);
		$nb_words = 0;
		foreach ($keyWords as $word) {
			if (in_array(strtolower($word), $list_radicaux)){
				$nb_words++;
			}
		}
		array_push($results, $row["nom"]);
	}

	return $results;
}

function getNombreTotalDocumentsPertinents($requete) {
	$requete = "SELECT 
					SUM(pertinence) nb 
				FROM 
					web_sem.requetes r, 
					web_sem.pertinences p 
				WHERE r.id = p.id_requete 
				  AND intitule LIKE '" . $requete . "'";
	
	$statement = getConnexion()->query($requete);
	$row = $statement->fetch();
	return $row["nb"];
}

function getNombreDocumentsPertinentsSelectionne($requete, $liste_documents) {
	$liste_string = "'" . join("', '", $liste_documents) . "'";
	
	$requete = "SELECT 
					SUM(pertinence) nb 
				FROM 
					web_sem.requetes r, 
					web_sem.pertinences p 
				WHERE r.id = p.id_requete 
				  AND document IN (" . $liste_string . ") 
				  AND intitule LIKE '" . $requete . "'";
	$db = getConnexion();
	$statement = $db->query($requete);
	$row = $statement->fetch();
	return $row["nb"];
}

function getRappels($query, $liste_documents, $filtre = -1) {
	
	if($filtre == -1){
		$filtre = count($liste_documents);
	}
	
	$liste_documents = array_slice($liste_documents, 0, $filtre);
	
	$NombreTotalDocumentsPertinents = getNombreTotalDocumentsPertinents($query);
	$nombreDocumentsPertinentsSelectione = getNombreDocumentsPertinentsSelectionne($query, $liste_documents);
	$rappel = $nombreDocumentsPertinentsSelectione / $NombreTotalDocumentsPertinents;
	
	return $rappel;
	
}

function robertson($poid) {

	
	
	return $rappel;
	
}

function getPrecision($requete, $liste_documents, $filtre = -1) {
	if ($filtre == -1) {
		$filtre = count($liste_documents);
	}
	
	$documents_base = array_slice ($liste_documents , 0 , $filtre);
	
	return getNombreDocumentsPertinentsSelectionne($requete, $documents_base) / count($documents_base);
}

function troncA7($mot) {
	$endIndex = min(strlen($mot), 7);
	if ($endIndex < 0) {
		$endIndex = 0;
	}
	return substr($mot, 0, $endIndex);
}

function listeWord($word) {
	$request = array();
	$eachElement = split(" +", $word);
	$stopWord = getStopWord();
	
	foreach ($eachElement as $value){
		$temp = troncA7($value);
		if(in_array($temp, $stopWord) === false) {
			array_push($request, $temp);
		}
	}
	
	return $request;
}

function getStopWord() {
	$requete = "SELECT word FROM stop_word";
	$statement = getConnexion()->query($requete);
	$stopWord = array();
	
	while ($row = $statement->fetch()) {
		array_push($stopWord, $row["word"]);
	}

	return $stopWord;
}

?>
